package com.kkb.shopcenter.controller;


import com.kkb.shopcenter.common.common.HttpResult;
import com.kkb.shopcenter.util.FastDFSClientUtil;
import org.apache.dubbo.common.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/api")
public class ImageUploadController {

    @Resource
    private FastDFSClientUtil fastDFSClientUtil;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public HttpResult  upload(@RequestParam("file") MultipartFile file){
        String result = null;
        try {
            String path = fastDFSClientUtil.uploadImage(file);
            if(StringUtils.isNotEmpty(path)){
                result = path;
            }else{
                result = "fail";
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = "error";
        }
        return HttpResult.success(result);
    }

    @RequestMapping(value = "/deleteImage",method = RequestMethod.GET)
    public HttpResult deleteImage(@RequestParam("fileUrl") String fileUrl){
        fastDFSClientUtil.deleteFile(fileUrl);
        return HttpResult.success("OK");
    }

}
